﻿//*** Guidance generated code ***//
using System;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.Linq;
using System.Web.Http;
using System.Web.Mvc;

using ProjectBase.Core;
using ProjectBase.Core.Utils;
using ProjectBase.Core.Web;
using ProjectBase.Mvc;
using ProjectBase.Mvc.Controllers;

using $prjsn$.Common;
using $prjsn$.Common.Business.Services;
using $prjsn$.Common.Controllers;
using $prjsn$.Common.Models;

namespace $safeprojectname$.Controllers
{
    public class HomeController : WebController<IHomeModel>, IHomeController
    {
        #region help
        public ActionResult ApiOverview()
        {
            var apiExplorer = GlobalConfiguration.Configuration.Services.GetApiExplorer();
            return View(apiExplorer);

        }
        #endregion

        #region Clear the Cache - Refresh
        /// <summary>
        ///     Clears all the static data, and forces the refresh from data storage.
        ///     Redirects to the welcome screen
        /// </summary>
        /// <returns></returns>
        public virtual ActionResult Refresh()
        {
            ResetCacheFacade.ResetStaticCaches(this);
            return RedirectToAction(Str.Actions.Default);
        }
        #endregion Clear the Cache

        #region Index (the controllers's Gate)
        /// <summary>
        ///     Application entry point
        /// </summary>
        public override ActionResult Index()
        {
            //Model.Article = ArticleFacade.GetByCode(Str.Controllers.Home);

            this.AddAction(Str.Business.Home.Actions.Refresh, Url);

            return View(Str.Actions.Default, Model);
        }
        #endregion Index (the controllers's Gate)

        #region Lang
        /// <summary>
        ///     Renders view with the list of available languages to be selected
        /// </summary>
        /// <returns></returns>
        public virtual ActionResult SelectLanguage()
        {
            return View(Model);
        }

        /// <summary>
        ///     Changes Culture to selected language.
        ///     If possible, redirects to page, where this action was asked
        ///     (user should feel like if nothing has changed)
        /// </summary>
        /// <param name = "id"></param>
        /// <returns></returns>
        public virtual ActionResult LanguageSelected(short id)
        {
            if (id.Is())
            {
                var lang = Project.GetLanguages().Where(l => l.ID.Equals(id)).SingleOrDefault();
                if (lang.Is())
                {
                    var langName = lang.LanguageName;
                    // when new language is set, go back to original path
                    var originalAction = Request.Params[Str.Business.Home.Names.OriginalAction];
                    if (originalAction.IsNotEmpty())
                    {
                        var routeData = originalAction.GetRouteData();
                        routeData.Values["lang"] = langName;
                        return RedirectToRoute(routeData.Values);
                    }
                    RouteData.Values["lang"] = langName;
                }
            }

            return RedirectToAction(Str.Actions.Default);
        }

        #endregion Lang

        #region public properties
        public override string ControllerName
        {
            get { return Str.Controllers.Home; }
        }
        #endregion  properties

        #region DI (IoC)
        /// <summary>
        ///     IResetCacheFacade
        /// </summary>
        public virtual IResetCacheFacade ResetCacheFacade { protected get; set; }

        public virtual IArticleFacade ArticleFacade { protected get; set; }
        #endregion DI (IoC)
    }
}